//
//  NSDate+TCTStringFormatter.swift
//  SwiftTestDemo
//
//  Created by John on 2018/5/22.
//  Copyright © 2018年 company. All rights reserved.
//

import Foundation

extension Date{
    
    func stringWithDateFormat(_ dateFormat:String) -> String? {
        
        let dateFormatter:DateFormatter = TCTFormatter.sharedDateFormatter()
        
        dateFormatter.dateFormat = dateFormat
        
        return dateFormatter.string(from: self)
        
    }
    
    func stringBeforeDate() -> String? {
        
        var timeInterval:TimeInterval = self.timeIntervalSinceNow
        
        timeInterval = -timeInterval
        
        var temp:Int = 0
        
        var result:String
        
        if timeInterval < 60 {
            
            result = "刚刚"
            
        }
        else if (timeInterval / 60) < 60{
            
            temp = Int(timeInterval / 60)
            
            result = "\(temp)分前"
            
        }
        else if (temp / 60) < 24{
            
            temp = temp / 60
            
            result = "\(temp)小时前"
            
        }
        else if (temp / 24) < 30{
            
            temp = temp / 24
            
            result = "\(temp)天前"
            
        }
        else if (temp / 30) < 12{
            
            temp = temp / 30
            
            result = "\(temp)月前"
            
        }
        else{
            
            temp = temp / 12
            
            result = "\(temp)年前"
            
        }
        
        return result
        
    }
    
    func convertDateToNewDateFormat(_ dateFormat:String) -> Date? {
        
        if dateFormat.count > 0{
            
            let covertDateString:String! = self.stringWithDateFormat(dateFormat)
            
            let convertDate:Date? = covertDateString.dateWithDateFormat(dateFormat)
            
            return convertDate
            
        }
        else{
            
            return self
            
        }
    }
    
}
